package com.jiamao.goplay.ui.photo;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProviders;
import androidx.viewpager.widget.ViewPager;

import com.gyf.immersionbar.ImmersionBar;
import com.jiamao.goplay.BR;
import com.jiamao.goplay.R;
import com.jiamao.goplay.adapter.UserWanToGoAdapter;
import com.jiamao.goplay.app.AppViewModelFactory;
import com.jiamao.goplay.base.BaseMVVMFragment;
import com.jiamao.goplay.databinding.PhotoViewLayoutBinding;
import com.jiamao.goplay.ui.photo.vm.PhotoViewItmeVM;
import com.jiamao.goplay.ui.photo.vm.PhotoViewVM;

import java.util.ArrayList;

public class PhotoViewFragment extends BaseMVVMFragment<PhotoViewLayoutBinding, PhotoViewVM> {

    private int count =0;
    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.photo_view_layout;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }


    @Override
    public PhotoViewVM initViewModel() {
        //使用自定义的ViewModelFactory来创建ViewModel，如果不重写该方法，则默认会调用LoginViewModel(@NonNull Application application)构造方法
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getActivity().getApplication());
        return ViewModelProviders.of(this, factory).get(PhotoViewVM.class);
    }


    @Override
    public void initData() {
        super.initData();


        binding.setAdapter(new UserWanToGoAdapter(getContext()));
        ImmersionBar.with(this). keyboardEnable(false).statusBarDarkFont(false).titleBar(binding.getRoot().findViewById(R.id.toolbar)).init();
        TextView titleTv =  binding.getRoot().findViewById(R.id.tvTitle);
        titleTv.setText("");
        binding.getRoot().findViewById(R.id.iv_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getActivity().finish();
            }
        });


         ArrayList<String> list =  getArguments().getStringArrayList("list");
        for (String img : list){
            PhotoViewItmeVM item = new PhotoViewItmeVM(viewModel,img);
            viewModel.observableList.add(item);
        }
        count = list.size();

        binding.viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                binding.titleRight.setText((position+1)+"/"+count);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        int index =   getArguments().getInt("index");

        binding.viewPager.postDelayed(new Runnable() {
            @Override
            public void run() {
                binding.viewPager.setCurrentItem(index,false);
            }
        },100);
        binding.titleRight.setText((index+1)+"/"+count);

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

    }
}
